package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IBDMSaleLinesDao;
import com.qymerge.manage.base.dao.ICardDao;
import com.qymerge.manage.base.dao.IMetaLogsDao;
import com.qymerge.manage.base.ro.BDMSaleLinesRO;
import com.qymerge.manage.base.ro.MetaLogsRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 *<p>Title: BDMSaleLinesDaoImpl</p>
 *<p>Description: 优惠券管理</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:54
 */
@Repository
public class BDMSaleLinesDaoImpl extends BaseDao implements IBDMSaleLinesDao {

    @Autowired
    private IMetaLogsDao metaLogsDao;

    private String dwd_MCRS_BDM_SaleLines = "SELECT Id,BillId,BillCode,BillItemId,CompanyId,CompanyName,CompanyCode,CustomerId,CustomerName,CustomerCode,CustomerInternalCompanyId,CustomerInternalCompanyName,CustomerInternalCompanyCode,SalesCostCenterId,SalesCostCenterName,PeriodId,PeriodYear,PeriodQuarter,PeriodMonth,PeriodCode,BizDate,MaterialId,MaterialName,MaterialCode,Quantity,PackQuantity,StdPackQuantity,SalesPrice,SalesAmount,RevenueExternalAmount,RevenueInternalAmount,RevenueAccountViewCode,RevenueAccountViewName,RevenueGroupAccountViewCode,CostExternalAmount,CostInternalAmount,CostAccountViewCode,CostAccountViewName,CostGroupAccountViewCode,DataSource,JournalEntryId,Etl_Last_Date,PeriodDate,Source FROM dwd_MCRS_BDM_SaleLines WHERE 1=1 ";

    @Override
    public List list(BDMSaleLinesRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_BDM_SaleLines;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getBillCode())) {
            sql += " and BillCode like :BillCode";
            paramMap.put("BillCode", "%" + ro.getBillCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCompanyCode())) {
            sql += " and CompanyCode like :CompanyCode";
            paramMap.put("CompanyCode", "%" + ro.getCompanyCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCompanyName())) {
            sql += " and CompanyName like :CompanyName";
            paramMap.put("CompanyName", "%" + ro.getCompanyName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCustomerCode())) {
            sql += " and CustomerCode like :CustomerCode";
            paramMap.put("CustomerCode", "%" + ro.getCustomerCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCustomerName())) {
            sql += " and CustomerName like :CustomerName";
            paramMap.put("CustomerName", "%" + ro.getCustomerName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getSalesCostCenterName())) {
            sql += " and SalesCostCenterName like :SalesCostCenterName";
            paramMap.put("SalesCostCenterName", "%" + ro.getSalesCostCenterName() + "%");
        } if (StringUtils.isNotBlank(ro.getMaterialCode())) {
            sql += " and MaterialCode like :MaterialCode";
            paramMap.put("MaterialCode", "%" + ro.getMaterialCode() + "%");
        } if (StringUtils.isNotBlank(ro.getMaterialName())) {
            sql += " and MaterialName like :MaterialName";
            paramMap.put("MaterialName", "%" + ro.getMaterialName() + "%");
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<BDMSaleLinesRO>(BDMSaleLinesRO.class));
    }

    @Override
    public int total(BDMSaleLinesRO ro) {
        String sql = "select count(1) from dwd_MCRS_BDM_SaleLines where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getBillCode())) {
            sql += " and BillCode like :BillCode";
            paramMap.put("BillCode", "%" + ro.getBillCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCompanyCode())) {
            sql += " and CompanyCode like :CompanyCode";
            paramMap.put("CompanyCode", "%" + ro.getCompanyCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCompanyName())) {
            sql += " and CompanyName like :CompanyName";
            paramMap.put("CompanyName", "%" + ro.getCompanyName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCustomerCode())) {
            sql += " and CustomerCode like :CustomerCode";
            paramMap.put("CustomerCode", "%" + ro.getCustomerCode() + "%");
        }
        if (StringUtils.isNotBlank(ro.getCustomerName())) {
            sql += " and CustomerName like :CustomerName";
            paramMap.put("CustomerName", "%" + ro.getCustomerName() + "%");
        }
        if (StringUtils.isNotBlank(ro.getSalesCostCenterName())) {
            sql += " and SalesCostCenterName like :SalesCostCenterName";
            paramMap.put("SalesCostCenterName", "%" + ro.getSalesCostCenterName() + "%");
        } if (StringUtils.isNotBlank(ro.getMaterialCode())) {
            sql += " and MaterialCode like :MaterialCode";
            paramMap.put("MaterialCode", "%" + ro.getMaterialCode() + "%");
        } if (StringUtils.isNotBlank(ro.getMaterialName())) {
            sql += " and MaterialName like :MaterialName";
            paramMap.put("MaterialName", "%" + ro.getMaterialName() + "%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public BDMSaleLinesRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_BDM_SaleLines);
        sql.append("and Id=?");
        RowMapper<BDMSaleLinesRO> rowMapper = new BeanPropertyRowMapper<BDMSaleLinesRO>(BDMSaleLinesRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List list2(BDMSaleLinesRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();
        String sql =  "call getBDMSaleLinesByBillCode(?,?,?)";
        List<BDMSaleLinesRO> List = getJdbcTemplate().query(sql, new Object[]{ro.getBillCode(), skip, size}, new BeanPropertyRowMapper<BDMSaleLinesRO>(BDMSaleLinesRO.class));
        return List;
    }


}
